<?

$num_rows_to_upload = 15;
$formId = 223; 
$documentTypeCode = 'docProduct';

$authInfo = extractObjectWithKeyFromArray('authInfo', $_SESSION);

$formDescriptorTableWriter = new CmsTableWriter(CMS_FORM_DESC_FORMAT, $db);
$formDescription = $formDescriptorTableWriter->readItem($formId);
$element = new CmsFormElement($formDescription, $db);

unset($formDescription->formid);
unset($formDescription->formname);
unset($formDescription->usertype);
unset($formDescription->editmode);
unset($formDescription->uid);
unset($formDescription->gid);
unset($formDescription->permission);
unset($formDescription->sidebarmodule);
unset($formDescription->activemodule);
unset($formDescription->contentmodule);
unset($formDescription->modeaccess);
unset($formDescription->documentcode);
unset($formDescription->documenttypecode);
unset($formDescription->categorycode);
unset($formDescription->datelogged);
unset($formDescription->documentdate);
unset($formDescription->documentyear);
unset($formDescription->startdate);
unset($formDescription->enddate);
unset($formDescription->active);
unset($formDescription->extra1);
unset($formDescription->extra2);
unset($formDescription->extra3);
unset($formDescription->extra4);
unset($formDescription->extra5);
unset($formDescription->extra6);
unset($formDescription->extra7);
unset($formDescription->extra8);
unset($formDescription->displaygroup);

$base_fields = array();
foreach($formDescription as $fieldname=>$elementid){
	if($formDescription->$fieldname)
		array_push($base_fields,$fieldname);
}


$spec_fields = array('extra1','extra2','extra3','extra4','extra5');

$masterProductId = extractNumberWithKeyFromArray("masterProductId", $_GET, 0);
$masterProduct = $cmsDocument->openDocument($masterProductId);


	




$processMultipleUpload = extractNumberWithKeyFromArray("processMultipleUpload", $_POST, 0);

if($processMultipleUpload){
	
	$c = 0;
	for($i=0;$i<$num_rows_to_upload;$i++){
		
		if(!array_key_exists('title',$_POST) || !array_key_exists($i,$_POST['title']) || is_null(extractStringWithKeyFromArray($i, $_POST['title'], null))){
			continue;
		}
		
		$dataToSave = array();
		$dataToSave['documenttypecode'] = $documentTypeCode;
		$dataToSave['categorycode'] = $masterProduct->documentcode;
		$dataToSave['active'] = 1;
		
		$db_fields = array_merge($base_fields, $spec_fields);
		
		foreach($db_fields as $field){
			if(array_key_exists($field,$_POST))
				$dataToSave[$field] = extractStringWithKeyFromArray($i, $_POST[$field]);
		}
		

		
		//myPrintR($dataToSave);
		
		$cmsWriter = new CmsWriter( $formId, null, $dataToSave, $authInfo, &$db);	
		$res = $cmsWriter->processFormData();
		if ( $res->hasErrors){
			print '<span style="color:red">';
			myPrintR($res);
			print '</span>';
		}else{
		
			$c++;
		
		}
		
	}
	
	
	
	if($c>1)
		$msg = "$c products were saved successfully.<br /><br />";
	elseif($c==1)
		$msg = "$c product was saved successfully.<br /><br />";
	
	
	header("Location: /cms/html/index.php?fileNo=1&formid=223&masterProductId=".$masterProductId);
	exit;
}


$cmsDocument->flush();
$cmsDocument->addDocumentType("docMasterPro");
$cmsDocument->addSort("title");
$cmsDocument->addConstraint("active", "1");
$cmsDocument->resetIterator();

?>
<div id="cms_browse_heading" class="cms_browse_heading">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td valign="top" align="left">
	<h1>Add Multiple Products</h1>
      </td>
      <td align="right" valign="top"></td>
    </tr>
  </table>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>

<div>

<?if(isset($msg)) echo $msg;?>

<form name="select_master_product" method="get" class="cms_form">
<input type="hidden" name="fileNo" value="<?=$fileNo?>">
Master Product: &nbsp;
<select name="masterProductId" onchange="document.select_master_product.submit()">
<option value="0">-- Select master product --</option>
<?
	while($row = $cmsDocument->nextDocument()){
?> <option value="<?=$row->documentcode?>" <?if($masterProductId==$row->documentcode) print ' selected="selected"';?>><?=magicclean($row->title)?></option>
<?
	}
?>   
</select>
</form>
<br />



<?
if($masterProductId && $masterProduct){
?>
<div class="variableForm">
<form name="multiple_products" method="post" class="cms_form">
<input type="hidden" name="processMultipleUpload" value="1" />
<table border="0" cellspacing="2" cellpadding="1">
	<tr>
<?
	foreach($base_fields as $field){
		$e = $element->getElementDescriptor($formDescription->$field);
		
?>	 <th><?=magicclean($e->label)?></td>
<?
	}
?>	 
<?	 
	$column = 0;
	foreach($spec_fields as $field){
		if($masterProduct->$field){
			$column++;
?>	 <th><?=$masterProduct->$field?></td>
<?
		}
	}
?>	</tr>
<?
	for($j=0;$j<$num_rows_to_upload;$j++){
?>	<tr>
<?
		foreach($base_fields as $field){
?>
	 <td><input type="text" name="<?=$field?>[]" /></td>
<?
		}
		for($i=0;$i<$column;$i++){
?>	 <td><input type="text" name="<?=$spec_fields[$i]?>[]" /></td>
<?
		}
?>	</tr>
<?
	}
?>

</table>
<input type="image" name="submit" src="/cms/images/button_go.gif" style="float:right;padding-right:10px" />
</form>
</div>
<?
}
?>

</div>

